package com.int_tech.newTimeTask;

import com.int_tech.mession.dao.MessionDao;
import com.int_tech.mession.entity.Mession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.List;

/**
 * Created by liubochao on 2018/6/30.
 */
@Component
public class CheckMessionRepeatTimeTask {

    @Autowired
    private MessionDao messionDao;

    /**
     * 筛选重复任务单
     * @throws ParseException
     */
    @Scheduled(cron="0/10 * * * * ?")
    public void checkMessionRepeat1() throws ParseException {

        List<Mession> messionList = messionDao.findByIsAnsyCarTimeAndIsOver(-99,1);


        for (Mession mession : messionList)
        {
            List<Mession> messions = messionDao.findByStartOpenDoorTimeAndCarNumberAndCreateTimeBefore(mession.getStartOpenDoorTime(),mession.getCarNumber(),mession.getCreateTime());

            List<Mession> messions1 = messionDao.findByStartOpenDoorTimeAndCarNumberAndIdLessThan(mession.getStartOpenDoorTime(),mession.getCarNumber(),mession.getId());


            //如果任务列表为空可直接当做正常任务
            if((messions == null || messions.size() == 0)&&(messions1 == null || messions1.size() == 0))
            {
                mession.setIsAnsyCarTime(-98);
            }
            else
            {
                mession.setIsAnsyCarTime(-100);
            }
            messionDao.save(mession);
        }
    }

    /**
     * 筛选重复任务单
     * @throws ParseException
     */
    @Scheduled(cron="0/10 * * * * ?")
    public void checkMessionRepeat2() throws ParseException {

        List<Mession> messionList = messionDao.findByIsAnsyCarTimeAndIsOver(-98,1);


        for (Mession mession : messionList)
        {
            List<Mession> messions = messionDao.findByStartCloseDoorTimeAndCarNumberAndCreateTimeBefore(mession.getStartCloseDoorTime(),mession.getCarNumber(),mession.getCreateTime());

            List<Mession> messions1 = messionDao.findByStartCloseDoorTimeAndCarNumberAndIdLessThan(mession.getStartCloseDoorTime(),mession.getCarNumber(),mession.getId());


            //如果任务列表为空可直接当做正常任务
            if((messions == null || messions.size() == 0)&&(messions1 == null || messions1.size() == 0))
            {
                mession.setIsAnsyCarTime(-97);
            }
            else
            {
                mession.setIsAnsyCarTime(-100);
            }
            messionDao.save(mession);
        }
    }

    /**
     * 筛选重复任务单
     * @throws ParseException
     */
    @Scheduled(cron="0/10 * * * * ?")
    public void checkMessionRepeat3() throws ParseException {

        List<Mession> messionList = messionDao.findByIsAnsyCarTimeAndIsOver(-97,1);


        for (Mession mession : messionList)
        {
            List<Mession> messions = messionDao.findByEndOpenDoorTimeAndCarNumberAndCreateTimeBefore(mession.getEndOpenDoorTime(),mession.getCarNumber(),mession.getCreateTime());

            List<Mession> messions1 = messionDao.findByEndOpenDoorTimeAndCarNumberAndIdLessThan(mession.getEndOpenDoorTime(),mession.getCarNumber(),mession.getId());

            //如果任务列表为空可直接当做正常任务
            if((messions == null || messions.size() == 0)&&(messions1 == null || messions1.size() == 0))
            {
                mession.setIsAnsyCarTime(-96);
            }
            else
            {
                mession.setIsAnsyCarTime(-100);
            }
            messionDao.save(mession);
        }
    }


    /**
     * 筛选重复任务单
     * @throws ParseException
     */
    @Scheduled(cron="0/10 * * * * ?")
    public void checkMessionRepeat4() throws ParseException {

        List<Mession> messionList = messionDao.findByIsAnsyCarTimeAndIsOver(-96,1);


        for (Mession mession : messionList)
        {
            List<Mession> messions = messionDao.findByEndCloseDoorTimeAndCarNumberAndCreateTimeBefore(mession.getEndCloseDoorTime(),mession.getCarNumber(),mession.getCreateTime());

            List<Mession> messions1 = messionDao.findByEndCloseDoorTimeAndCarNumberAndIdLessThan(mession.getEndCloseDoorTime(),mession.getCarNumber(),mession.getId());

            //如果任务列表为空可直接当做正常任务
            if((messions == null || messions.size() == 0)&&(messions1 == null || messions1.size() == 0))
            {
                mession.setIsAnsyCarTime(0);
            }
            else
            {
                mession.setIsAnsyCarTime(-100);
            }
            messionDao.save(mession);
        }
    }
}
